package com.erdos.common.web;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * ---------------------------------------------------------------------------------
 Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : SimpleBaseAction.java    
 Package      : com.hp.common.web                                                                   
 @version     $Id: SimpleBaseAction.java 9124 2012-03-08 03:00:56Z hubin $                                                          
 @author duanxi
 @since  2008-10-16 
 */
public class SimpleBaseAction extends ActionSupport {
	
	public final static String PARAMS_KEY_PAGE_NO="pn";
	
	public final static String PARAMS_KEY_PAGE_LIMIT="limit";
	
	public final static String PARAMS_KEY_PAGE_OBJECT="page";

	private static final long serialVersionUID = -7515581273533743583L;

	protected final static int PAGE_LIMIT = 10;

	public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

	// 放置查询参数的Map对象
	private Map<String, String> qm = new HashMap<String, String>();

	// logger for subclass
	private static final Log logger = LogFactory.getLog(BaseAction.class);

	public Map<String, String> getQm() {
		return qm;
	}

	public void setQm(Map<String, String> qm) {
		this.qm = qm;
	}
	
	/**
	 * 在调用Serivce的分页方法之前，预处理分页相关参数
	 * @param request
	 * @return
	 * @throws Exception
	 * @author xiali2
	 * @since  2008-9-16
	 */
	protected Map<String, Object> preparePageParams(HttpServletRequest request) throws Exception{
		String query = request.getQueryString();
		String pageURL = request.getServletPath();
		if (query != null && query.length() > 0) {
			pageURL += "?" + query;
		}
		request.setAttribute("contextURL", URLEncoder.encode(pageURL, "UTF-8"));
		logger.debug("pageURL=" + request.getContextPath() + pageURL);
		request.setAttribute("pageURL", request.getContextPath() + pageURL);
		Map<String, Object> params = new HashMap<String, Object>();
		Set<Map.Entry<String, String>> p = qm.entrySet();
		for (Map.Entry<String, String> me : p) {
			String name = me.getKey();
			String value = me.getValue();
			if (StringUtils.isNotEmpty(value)) {
				params.put(name, value);
			}
		}
		logger.debug("params=" + params);
		int pageNo = 1;
		if (!StringUtils.isEmpty((String) qm.get(PARAMS_KEY_PAGE_NO))) {
			try {
				pageNo = Integer.parseInt((String) qm.get(PARAMS_KEY_PAGE_NO));
			} catch (RuntimeException e) {
			}
		}
		params.put(PARAMS_KEY_PAGE_NO, new Integer(pageNo));
		int limit = PAGE_LIMIT;
		if (!StringUtils.isEmpty((String) qm.get(PARAMS_KEY_PAGE_LIMIT))) {
			try {
				limit = Integer.parseInt((String) qm.get(PARAMS_KEY_PAGE_LIMIT));
			} catch (RuntimeException e) {
			}
		}
		params.put(PARAMS_KEY_PAGE_LIMIT, new Integer(limit));
		return params;
	}
	
}
